package com.eightbears.beboy.ui.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.eightbears.beboy.MainActivity;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.CommonAPI;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.InputMethodUtils;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.entity.UserEntity;
import com.eightbears.beboy.entity.status.Status_Error;
import com.pnikosis.materialishprogress.ProgressWheel;
import com.socks.library.KLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class PasswordLoginActivity extends BaseActivity {

    @BindView(R.id.progress_wheel)
    ProgressWheel progress_wheel;
    @BindView(R.id.et_phone_num)
    EditText et_phone_num;
    @BindView(R.id.et_pass)
    EditText et_pass;
    @BindView(R.id.btn_pass_login)
    Button btn_pass_login;
    @BindView(R.id.tv_forget)
    TextView tv_forget;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;

    private String phoneNum;
    private String pass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_login);
        initView();
        initData();
        initEvent();
    }


    @Override
    protected void initView() {
        ButterKnife.bind(this);
        tv_title.setText(R.string.text_title_pass_login);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }

    @OnClick(R.id.btn_pass_login)
    void login() {
        if (validInput()) {
            progress_wheel.setVisibility(View.VISIBLE);
            replaceButtonUnable();
            passLogin();
        }
    }

    @OnClick(R.id.tv_forget)
    void forgetPass() {
        startLocalActivity(this, ForgetActivity.class);
    }

    public void passLogin() {
        OkHttpUtils.post().url(CommonAPI.URL_LOGIN)
                .addParams("id", phoneNum)
                .addParams("key", pass)
                .addParams("auth_type", "password")
                .addParams("third_party", "weixin")
                .addParams("grant_type", "password")
                .addParams("refresh_token", "")
                .addParams("unique_id", CommonUtils.getAndroidId(getApplication()))
                .build().execute(new StringCallback() {
            @Override
            public void onResponse(String response) {
                UserEntity userEntity = GsonUtil.fromJson(response, UserEntity.class);
                if (userEntity.getStatus_code().equals(CommonAPI.CONNECTION_STATUS_SUCCESS)) {
                    progress_wheel.setVisibility(View.GONE);
                    SPUtil.saveUserInfo2Local(userEntity);
                    InputMethodUtils.closeInputMethod(getApplication(), et_pass);
                    startLocalActivityAndClear(PasswordLoginActivity.this, MainActivity.class);
                    finish();
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                KLog.e(e.getMessage());
                progress_wheel.setVisibility(View.GONE);
                replaceButtonClickable();
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError(PasswordLoginActivity.this, status_error);
            }
        });
    }

    private boolean validInput() {
        phoneNum = et_phone_num.getText().toString().trim();
        pass = et_pass.getText().toString().trim();

        if (TextUtils.isEmpty(phoneNum)) {
            ShowToast.showShortToast(getText(R.string.alert_please_input_phone_num).toString());
            return false;
        }

        if (!CommonUtils.isPhoneNum(phoneNum)) {
            ShowToast.showShortToast(getText(R.string.error_phone_num_input).toString());
            return false;
        }

        if (TextUtils.isEmpty(pass)) {
            ShowToast.showShortToast(getText(R.string.alert_please_input_pass).toString());
            return false;
        }

        return true;
    }

    private void replaceButtonClickable() {
        btn_pass_login.setTextColor(getResources().getColor(R.color.text_color_fff));
        btn_pass_login.setText(getText(R.string.text_login));
        btn_pass_login.setBackgroundDrawable(getResources().getDrawable(R.drawable.background_circle_bead_blue_style));
        btn_pass_login.setFocusable(true);
        btn_pass_login.setClickable(true);
    }

    private void replaceButtonUnable() {
        btn_pass_login.setTextColor(getResources().getColor(R.color.text_other_info_color));
        btn_pass_login.setText(R.string.alert_login);
        btn_pass_login.setBackgroundDrawable(getResources().getDrawable(R.drawable.background_gray_unable));
        btn_pass_login.setFocusable(false);
        btn_pass_login.setClickable(false);
    }

    public EditText getEt_pass() {
        return et_pass;
    }
}
